package com.itheima.entry;

import com.itheima.controller.StudentController;
import com.itheima.domain.Student;

import java.util.Scanner;

public class Entry {
    private static Scanner sc = new Scanner(System.in);
    private static StudentController studentController = new StudentController();

    public static void main(String[] args) {


        while (true){
            System.out.println("---------------------黑马学生信息管理系统-------------------");
            System.out.println("请输入你的选择：1.登录  2.注册  3.查看  4.修改  5.删除  6.退出 ");
            switch (sc.nextInt()){
                case 1:
                    //System.out.println("登录");
                    while (true) {
                        System.out.println("请输入您的账号：");
                        String id = sc.next();
                        System.out.println("请输入您的密码：");
                        String password = sc.next();
                        Student stu = new Student(id, password);
                        boolean flag = studentController.login(stu);
                        if (flag) {
                            System.out.println("登录成功");
                            //输出详细信息
                            Student stu1 = studentController.print();
                            System.out.println("姓名："+stu1.getName());
                            System.out.println("年龄："+stu1.getAge());
                            System.out.println("所属学科："+stu1.getSubject());
                            break;
                        }else {
                            System.out.println("账号或密码错误，请重新输入！");
                        }
                    }
                    break;

                case 2:
                    //System.out.println("注册");
                    String id;
                    while (true) {
                        System.out.println("请输入您的账号：");
                        id = sc.next();
                        //*需要一个判断，该id是否重复
                        boolean flag = studentController.judge(id);
                        if (!flag){
                            break;
                        }else {
                            System.out.println("账号已存在，请重新输入!");
                        }
                    }
                    //调用student方法，传入id，封装成一个Student对象返回
                    Student stu = student(id);
                    studentController.addStudent(stu);
                    break;

                case 3:
                    //System.out.println("查看");
                    studentController.findAllStudents();
                    break;

                case 6:
                    System.out.println("退出");
                    System.exit(0);

                case 4:
//                    System.out.println("修改");
                    //判断账号存不存在
                    String upid = exist();
                    //执行封装对象流程
                    Student upstu = student(upid);
                    boolean flag = studentController.updateStudent(upstu);
                    if (flag){
                        System.out.println("修改成功");
                    }else {
                        System.out.println("修改失败");
                    }
                    break;

                case 5:
                    //System.out.println("删除");
                    //判断账号存不存在
                    String delid = exist();
                    boolean flag1 = studentController.deleteStudent(delid);
                    if (flag1){
                        System.out.println("删除成功");
                    }else {
                        System.out.println("删除失败");
                    }
                    break;
                default:
                        System.out.println("您输入的指令有误，请重新输入！");
            }
        }
    }

    //判断账号是否存在
    private static String exist(){
        String upid;
        while (true) {
            System.out.println("请输入您要修改的账号：");
            upid = sc.next();
            //查找id是否存在
            boolean flag = studentController.judge(upid);
            if (!flag) {
                System.out.println("账号不存在，请重新输入！");
            }else {
                break;
            }
        }
        return upid;
    }

    //封装成Student对象
    private static Student student(String id){
        System.out.println("请输入您的姓名：");
        String name = sc.next();
        System.out.println("请输入您的年龄：");
        String age = sc.next();
        System.out.println("请输入您的学科：");
        String subject = sc.next();
        System.out.println("请输入您的密码：");
        String password = sc.next();
        return new Student(id,name,age,subject,password);
    }
}
